package newx.com
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.system.ApplicationDomain;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.ControlRegedit;
	import newx.com.control.NxClip;
	import newx.com.control.NxToolTips;
	import newx.com.control.Scroll.NxScrollPanel;
	import newx.com.control.button.NxButton;
	import newx.com.control.comboBox.NxComboBox;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.list.NxList;
	import newx.com.control.progress.NxProgressBar;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.tab.NxTab;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxInput;
	import newx.com.control.text.NxLabel;
	import newx.com.control.text.NxText;
	import newx.com.control.window.NxWindow;
	
	/**
	 * ui管理器，用于创建，维护ui实例
	 * @author wangjun
	 * 
	 */	
	public class NxUIManage
	{
		private static 	var _self			:NxUIManage;
		
		/**
		 * ui类包地址
		 * */
		private var _uiClassPath	:String		=	"";
		
		/**
		 * 应用程序域
		 * */
		private var _appDomain		:ApplicationDomain;
		
		private var _uiInstance	:BaseObject	=	new BaseObject();
		
		public function NxUIManage()
		{
			new ControlRegedit();
		}

//		public function get uiClassPath():String
//		{
//			return _uiClassPath;
//		}

//		public function set uiClassPath(value:String):void
//		{
//			_uiClassPath = value+".";
//		}
		
		/**
		 * 初始化ui管理器 
		 * @param uiClassPath
		 * @param appDomain
		 * 
		 */		
		public function Init(uiClassPath:String,appDomain:ApplicationDomain):void
		{
			_uiClassPath 	=	uiClassPath+".";
			_appDomain		=	appDomain;
			
			//初始化tips模块
			NxToolTips.Init(GameEngine._gameApi.stage);
		}

		public static function getInstance():NxUIManage
		{
			if(_self == null)
			{
				_self = new NxUIManage();
			}
			return _self;
		}
		
//		/**
//		 * 创建ui实例。 
//		 * @param className
//		 * 
//		 */		
//		public function newUIInstance(className:String,appDomain:ApplicationDomain,pList:Array = null):Sprite
//		{
//			//此处创建的ui，必须继承NxSprite且实现接口newx.com.ui.IGameUI
//			var ui			:NxSprite;
//			var uiClass		:Class;
//			var uiClassName	:String	=	_uiClassPath+className;
//			
//			try
//			{
//				uiClass	=	appDomain.getDefinition(uiClassName) as Class;
//				ui		=	new uiClass().getInstance(pList);
//			}
//			catch(e:Error)
//			{
//				ui	=	null;
//				IO.traceError("NxUIManage::newUIInstance出错，界面类不存在或不合法。");
//			}
//			
//			if(ui != null)
//			{
//				initUI(ui,className);
//			}
//			return ui;
//		}
		
		public function InitUI(ui:Sprite,className:String):void
		{
			var	arrObj:Object	=	StaticIni.getIniObj(className);
			for(var p:String in arrObj)
			{
				if("controls"	==	p)
				{
					var controls:Array	=	arrObj[p].split(",");
					CreateUIControl(controls,ui,className);
				}
				else
				{
					ui[p]	=	arrObj[p];
				}
			}
//			ui.Init();
//			ui.mouseChildren = ui.mouseEnabled = ui.visible = false;
		}
		
		private function CreateUIControl(controls:Array,ui:Sprite,className:String):void
		{
			var parentSpace		:DisplayObjectContainer;
			
			var tSize			:uint	=	controls.length;
			
			//组件变量
			var controlsVar		:String;
			
			//组件类型
			var controlType		:String;
			
			//父对象
			var controlParent	:Object;
			
			//组件配置
			var creatObj		:Object;
			
			for(var i:uint = 0 ; i < tSize ; i ++)
			{
				//创建组件
				controlsVar		=	controls[i];
				
				creatObj		=	StaticIni.getIniObj(className+"_"+controlsVar);
				
				controlType		=	creatObj["className"];
				
				createControl(controlType,ui,controlsVar);
				
				controlParent	=	creatObj["parent"];
				
				if(null != controlParent && controlParent != "")
				{
					if(controlParent == "this")
					{
						//当配置成“this”时，显示对象为界面类本身
						parentSpace	=	ui	as DisplayObjectContainer;
					}
					else
					{
						parentSpace	=	ui[controlParent]	as DisplayObjectContainer;
					}
					
					parentSpace.addChild(ui[controlsVar]);
				}
				
				
				for(var p:String in creatObj)
				{
					if("parent" != p && "create" != p && "className" != p)
					{
						//判断参数
						if(ui[controlsVar][p] is Array)
						{
							ui[controlsVar][p]	=	checkArrLang(getArrayByStr(creatObj[p]));
						}
						else if(ui[controlsVar][p] is Boolean)
						{
							if(creatObj[p] == 0 || creatObj[p] == "false")
							{
								ui[controlsVar][p]	=	false;
							}
							else
							{
								ui[controlsVar][p]	=	true
							}
						}
						else if(ui[controlsVar][p] is String)
						{
							ui[controlsVar][p]	= getLangStr(creatObj[p]);
						}
						else if(ui[controlsVar][p] is int)
						{
							ui[controlsVar][p]	= int(creatObj[p])
						}
						else if(ui[controlsVar][p] is Number)
						{
							ui[controlsVar][p]	= Number(creatObj[p]);
						}
					}
				}					
			}
		}
		
		private function getArrayByStr(val:String):*
		{
			var pattern:RegExp = /^[$].*[$]$/;
			
			if(val.match(pattern))
			{
				if(val.length > 2)
				{
					var	strSub:String	=	val.substr(1, val.length - 2);
					return	strSub.split(";");	
				}
				else
				{
					return	new Array();
				}
			}
			return null;		
		}
		
		private function getLangStr(val:String):String
		{
			var pattern:RegExp = /^[##].+[##]$/;
			
			if(	val.match(pattern))
			{						
				var strSub:String	=	val.substr(2, val.length - 4);
				return StaticIni.getSwfLangStr(strSub);							
			}
			return val;
		}
		
		private function checkArrLang(arr:Array):Array
		{
			if(arr == null || arr.length == 0)
			{
				return null;
			}
			var tSize:uint	=	arr.length;
			for(var i:uint = 0 ; i < tSize ; i++)
			{
				arr[i]	=	getLangStr(arr[i])
			}
			return arr;
		}
		
		private function createControl(classname:String,ui:Sprite,pName:String):void
		{
			var tControl:NxUIComponent;
			switch(classname)
			{
				case "NxButton":
					tControl	=	new NxButton();
					break;
				case "NxComboBox":
					tControl	=	new NxComboBox();
					break;
				case "NxViewGroup":
					tControl	=	new NxViewGroup();
					break;
				case "NxList":
					tControl	=	new NxList();
					break;
				case "NxProgressBar":
					tControl	=	new NxProgressBar();
					break;
				case "NxRadio":
					tControl	=	new NxRadio();
					break;
				case "NxScrollPanel":
					tControl	=	new NxScrollPanel();
					break;
				case "NxTab":
					tControl	=	new NxTab();
					break;
				case "NxTable":
					tControl	=	new NxTable();
					break;
				case "NxInput":
					tControl	=	new NxInput();
					break;
				case "NxLabel":
					tControl	=	new NxLabel();
					break;
				case "NxText":
					tControl	=	new NxText();
					break;
				case "NxWindow":
					tControl	=	new NxWindow();
					break;
				case "NxClip":
					tControl	=	new NxClip();
					break;
			}
			ui[pName]	=	tControl;
		}
	}
}
